var home=angular.module('module.home',[
    'ui.router',
    'module.adminInfo',
    'ngResource',
    'module.apply',
    'module.changePwd',
    'module.goodsManage',
    'module.goodsMoneyStatistic',
    'module.goodsOrder',
    'module.goodsSendcard',
    'module.goodsStatistic',
    'module.ktvRecharge',
    'module.passwordManage',
    'module.login',
    'module.memberList',
    'module.noticeList',
    'module.orderCreate',
    'module.print',
    'module.qrcodeDesk',
])
    .run(['$rootScope', '$state', '$stateParams', function ($rootScope, $state, $stateParams) {
        $rootScope.$state = $state;
        $rootScope.$stateParams = $stateParams;
        console.log("angular started")
    }])
    .config(['$stateProvider','$urlRouterProvider',function ($stateProvider,$urlRouterProvider) {
        $urlRouterProvider.otherwise('/home/adminInfo');
        $stateProvider
            .state('home',{
                // 'abstract':true,
                url:"/home",
                templateUrl:"template/angularjs.template.html",
                resolve:{
                    resData:['HomeService',function (HomeService) {
                        return HomeService.getBaseData().$promise
                    }]

                },
                controller:'HomeController',
                controllerAs:'vm'
            })
    }])
    .controller('HomeController',['$scope','resData',function ($scope,resData) {
        data=resData;
        var vm =this;
        vm.data=data.data;
        console.log("vm.data",vm.data);

        adminUser=vm.data.adminUser;
        moneyStatisticsPassword=vm.data.moneyStatisticsPassword;
        goodsStatisticsPassword=vm.data.goodsStatisticsPassword;
        rechargePassword=vm.data.rechargePassword;

        vm.shopList=vm.data.shopList.shopList;
        vm.adminUser=vm.data.adminUser;
        vm.cmenu=vm.data.menuList;// 这里本来还有一个if_exists，但是不知道什么用就暂时先删掉了
        initialize();
        function initialize () {
            console.log(53)
            pathname = window.location.pathname;
            SLE=$("#shopList");  //给商户列表下拉框一个别名是shopListElement的缩写，方便使用
            checked=false;  //还不知道有什么用
            shop=vm.shopList[0];
            shopId=shop.shopId;  //全局变量，shopId所有页面商户ID都可以从这里取
            SLE.data("shopId",shopId);

//_____________________________样式________________________________________
            $("#shopName").html(shop.shopName);

            $("#shopListBtn,#shopName").click(function () {SLE.toggle();});


            $(".mlist").find("[href='"+pathname+"']").addClass("choose_mlist");

//实现边侧栏收放
            vm.mlistClick=function ($event) {
                $event.preventDefault();
                $(this).next().slideToggle();
            };

//多店版本边侧栏移除后台下单
            if (vm.shopList.length>1)$("a[href='/store/orderCreate']").parent().remove();
            //_________________________________________________________________________
            //消息提示函数
            (function getMsgCount() {
                $.getJSON('/store/noticeCount', function(res){
                    if (!res.succeed) { if(res.message)toast(res.message); return false;}
                    // console.log(res);
                    var para = res.data, html = '';
                    // console.log("para",para);

//          修改提醒消息的数量
                    $('.rightbox .volume sup').html(res.data.iCount);

//          如果提醒消息不为零则创建消息提醒弹窗
                    if (para.remind_count>0 || para.roomList.length>0){
                        if (para.remind_count>0) html += '您有<span class="red">'+para.remind_count+'</span>个新的订单，';
                        if (para.roomList.length>0) {
                            var room = [];
                            $.each(para.roomList, function(i, vo){
                                room.push(vo.roomId);
                            });
                            html+='有客户催单了，包厢号：<span class="red">'+room.join('，')+'</span>，';
                        }
                        winAlert({
                            title:'您有新的消息',
                            body:html+'请及时处理',
                            btn:"查看",
                            cancel:'忽略',
                            fun:function(){
                                location.href='/store/goodsOrder?orderNo='+para.orderNo;
                            }
                        });
                    }
//          每过3秒就判断一般有没有提醒消息
//                     setTimeout(function(){getMsgCount();}, 3000);
                });
            })();

            vm.shopListClick=function (shopId,shopName) {
                $("#shopName").html(shopName);
                SLE.data("shopId",shopId);
                $
                (".gn_tabs li.active a").trigger("click");
                // loadpage();
                $('.search').trigger("click");
            };
        }
    }])
    .service('HomeService',['$resource',function ($resource) {
        //获取基础数据
        this.getBaseData=function (success,error) {
            var resource=$resource(baseApiUrl+'store/AgularBaseData');
            return resource.get({},success,error)
        }
    }]);

